package org.gdstash.ui.character;

import org.gdstash.character.GDChar;
import org.gdstash.character.GDCharUID;
import org.gdstash.ui.GDStashFrame;
import org.gdstash.ui.util.AdjustablePanel;
import org.gdstash.ui.util.GDAbstractRiftPane;

import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.util.LinkedList;
import java.util.List;

public class GDCharRiftPane extends AdjustablePanel {
    private GDAbstractRiftPane pnlAct1;
    private GDAbstractRiftPane pnlAct2;
    private GDAbstractRiftPane pnlAct3;
    private GDAbstractRiftPane pnlAct4;
    private GDAbstractRiftPane pnlAct5;
    private GDAbstractRiftPane pnlAct6;
    private GDAbstractRiftPane pnlAct7;
    private int difficulty;

    public GDCharRiftPane(final int difficulty) {
        this.difficulty = difficulty;
        this.adjustUI();
        final JPanel pnlRift = this.buildRiftPanel();
        GroupLayout layout = null;
        GroupLayout.SequentialGroup hGroup = null;
        GroupLayout.SequentialGroup vGroup = null;
        layout = new GroupLayout(this);
        this.setLayout(layout);
        layout.setAutoCreateGaps(false);
        layout.setAutoCreateContainerGaps(false);
        hGroup = layout.createSequentialGroup();
        hGroup.addGroup(layout.createParallelGroup().addComponent(pnlRift));
        layout.setHorizontalGroup(hGroup);
        vGroup = layout.createSequentialGroup();
        vGroup.addGroup(layout.createParallelGroup().addComponent(pnlRift));
        layout.setVerticalGroup(vGroup);
    }

    @Override
    public void adjustUI() {
        Font fntLabel = UIManager.getDefaults().getFont("Label.font");
        fntLabel = fntLabel.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        final Border lowered = BorderFactory.createEtchedBorder(1);
        final Border raised = BorderFactory.createEtchedBorder(0);
        final Border compound = BorderFactory.createCompoundBorder(raised, lowered);
        this.setBorder(compound);
        if (this.pnlAct1 == null) {
            this.pnlAct1 = new GDCharRiftAct1Pane(this.difficulty, 1);
        } else {
            this.pnlAct1.adjustUI();
        }
        if (this.pnlAct2 == null) {
            this.pnlAct2 = new GDCharRiftAct2Pane(this.difficulty, 1);
        } else {
            this.pnlAct2.adjustUI();
        }
        if (this.pnlAct3 == null) {
            this.pnlAct3 = new GDCharRiftAct3Pane(this.difficulty, 1);
        } else {
            this.pnlAct3.adjustUI();
        }
        if (this.pnlAct4 == null) {
            this.pnlAct4 = new GDCharRiftAct4Pane(this.difficulty, 1);
        } else {
            this.pnlAct4.adjustUI();
        }
        if (this.pnlAct5 == null) {
            this.pnlAct5 = new GDCharRiftAct5Pane(this.difficulty, 1);
        } else {
            this.pnlAct5.adjustUI();
        }
        if (this.pnlAct6 == null) {
            this.pnlAct6 = new GDCharRiftAct6Pane(this.difficulty, 1);
        } else {
            this.pnlAct6.adjustUI();
        }
        if (this.pnlAct7 == null) {
            this.pnlAct7 = new GDCharRiftAct7Pane(this.difficulty, 1);
        } else {
            this.pnlAct7.adjustUI();
        }
    }

    private JPanel buildRiftPanel() {
        final JPanel panel = new JPanel();
        GroupLayout layout = null;
        GroupLayout.SequentialGroup hGroup = null;
        GroupLayout.SequentialGroup vGroup = null;
        layout = new GroupLayout(panel);
        panel.setLayout(layout);
        layout.setAutoCreateGaps(false);
        layout.setAutoCreateContainerGaps(false);
        hGroup = layout.createSequentialGroup();
        if (GDStashFrame.expansionForgottenGods) {
            hGroup.addGroup(layout.createParallelGroup().addComponent(this.pnlAct1)).addGroup(layout.createParallelGroup().addComponent(this.pnlAct2)).addGroup(layout.createParallelGroup().addComponent(this.pnlAct3)).addGroup(layout.createParallelGroup().addComponent(this.pnlAct4)).addGroup(layout.createParallelGroup().addComponent(this.pnlAct5)).addGroup(layout.createParallelGroup().addComponent(this.pnlAct6)).addGroup(layout.createParallelGroup().addComponent(this.pnlAct7));
        } else if (GDStashFrame.expansionForgottenGods) {
            hGroup.addGroup(layout.createParallelGroup().addComponent(this.pnlAct1)).addGroup(layout.createParallelGroup().addComponent(this.pnlAct2)).addGroup(layout.createParallelGroup().addComponent(this.pnlAct3)).addGroup(layout.createParallelGroup().addComponent(this.pnlAct4)).addGroup(layout.createParallelGroup().addComponent(this.pnlAct5)).addGroup(layout.createParallelGroup().addComponent(this.pnlAct6));
        } else if (GDStashFrame.expansionAshesOfMalmouth) {
            hGroup.addGroup(layout.createParallelGroup().addComponent(this.pnlAct1)).addGroup(layout.createParallelGroup().addComponent(this.pnlAct2)).addGroup(layout.createParallelGroup().addComponent(this.pnlAct3)).addGroup(layout.createParallelGroup().addComponent(this.pnlAct4)).addGroup(layout.createParallelGroup().addComponent(this.pnlAct5));
        } else {
            hGroup.addGroup(layout.createParallelGroup().addComponent(this.pnlAct1)).addGroup(layout.createParallelGroup().addComponent(this.pnlAct2)).addGroup(layout.createParallelGroup().addComponent(this.pnlAct3)).addGroup(layout.createParallelGroup().addComponent(this.pnlAct4));
        }
        layout.setHorizontalGroup(hGroup);
        vGroup = layout.createSequentialGroup();
        if (GDStashFrame.expansionForgottenGods) {
            vGroup.addGroup(layout.createParallelGroup().addComponent(this.pnlAct1).addComponent(this.pnlAct2).addComponent(this.pnlAct3).addComponent(this.pnlAct4).addComponent(this.pnlAct5).addComponent(this.pnlAct6).addComponent(this.pnlAct7));
        } else if (GDStashFrame.expansionForgottenGods) {
            vGroup.addGroup(layout.createParallelGroup().addComponent(this.pnlAct1).addComponent(this.pnlAct2).addComponent(this.pnlAct3).addComponent(this.pnlAct4).addComponent(this.pnlAct5).addComponent(this.pnlAct6));
        } else if (GDStashFrame.expansionAshesOfMalmouth) {
            vGroup.addGroup(layout.createParallelGroup().addComponent(this.pnlAct1).addComponent(this.pnlAct2).addComponent(this.pnlAct3).addComponent(this.pnlAct4).addComponent(this.pnlAct5));
        } else {
            vGroup.addGroup(layout.createParallelGroup().addComponent(this.pnlAct1).addComponent(this.pnlAct2).addComponent(this.pnlAct3).addComponent(this.pnlAct4));
        }
        layout.setVerticalGroup(vGroup);
        layout.linkSize(0, this.pnlAct1, this.pnlAct2);
        layout.linkSize(0, this.pnlAct1, this.pnlAct3);
        layout.linkSize(0, this.pnlAct1, this.pnlAct4);
        if (GDStashFrame.expansionAshesOfMalmouth) {
            layout.linkSize(0, this.pnlAct1, this.pnlAct5);
        }
        if (GDStashFrame.expansionForgottenGods) {
            layout.linkSize(0, this.pnlAct1, this.pnlAct6);
        }
        if (GDStashFrame.expansionForgottenGods) {
            layout.linkSize(0, this.pnlAct1, this.pnlAct7);
        }
        layout.linkSize(1, this.pnlAct1, this.pnlAct2);
        layout.linkSize(1, this.pnlAct1, this.pnlAct3);
        layout.linkSize(1, this.pnlAct1, this.pnlAct4);
        if (GDStashFrame.expansionAshesOfMalmouth) {
            layout.linkSize(1, this.pnlAct1, this.pnlAct5);
        }
        if (GDStashFrame.expansionForgottenGods) {
            layout.linkSize(1, this.pnlAct1, this.pnlAct6);
        }
        if (GDStashFrame.expansionForgottenGods) {
            layout.linkSize(1, this.pnlAct1, this.pnlAct7);
        }
        return panel;
    }

    public List<GDCharUID> getRiftList(final boolean found) {
        final List<GDCharUID> listAll = new LinkedList<GDCharUID>();
        List<GDCharUID> list = null;
        list = this.pnlAct1.getRiftList(found);
        if (list != null) {
            listAll.addAll(list);
        }
        list = this.pnlAct2.getRiftList(found);
        if (list != null) {
            listAll.addAll(list);
        }
        list = this.pnlAct3.getRiftList(found);
        if (list != null) {
            listAll.addAll(list);
        }
        list = this.pnlAct4.getRiftList(found);
        if (list != null) {
            listAll.addAll(list);
        }
        list = this.pnlAct5.getRiftList(found);
        if (list != null) {
            listAll.addAll(list);
        }
        list = this.pnlAct6.getRiftList(found);
        if (list != null) {
            listAll.addAll(list);
        }
        list = this.pnlAct6.getRiftList(found);
        if (list != null) {
            listAll.addAll(list);
        }
        return listAll;
    }

    public void setChar(final GDChar gdc) {
        this.pnlAct1.setChar(gdc);
        this.pnlAct2.setChar(gdc);
        this.pnlAct3.setChar(gdc);
        this.pnlAct4.setChar(gdc);
        this.pnlAct5.setChar(gdc);
        this.pnlAct6.setChar(gdc);
        this.pnlAct7.setChar(gdc);
    }

    public void updateChar(final GDChar gdc) {
        if (gdc == null) {
            return;
        }
        boolean changed = false;
        changed = (changed || this.pnlAct1.hasChanged());
        changed = (changed || this.pnlAct2.hasChanged());
        changed = (changed || this.pnlAct3.hasChanged());
        changed = (changed || this.pnlAct4.hasChanged());
        changed = (changed || this.pnlAct5.hasChanged());
        changed = (changed || this.pnlAct6.hasChanged());
        changed = (changed || this.pnlAct7.hasChanged());
        if (!changed) {
            return;
        }
        final List<GDCharUID> listAddAll = this.getRiftList(true);
        final List<GDCharUID> listRemoveAll = this.getRiftList(false);
        gdc.setRiftList(this.difficulty, listAddAll, listRemoveAll);
        this.pnlAct1.setChanged(false);
        this.pnlAct2.setChanged(false);
        this.pnlAct3.setChanged(false);
        this.pnlAct4.setChanged(false);
        this.pnlAct5.setChanged(false);
        this.pnlAct6.setChanged(false);
        this.pnlAct7.setChanged(false);
    }
}
